<?php

namespace backend\controllers;
use common\models\ar\PayMs;
use common\models\ar\PayValId;
class PayController extends BackendController
{
    public function actionIndex()
    {
        return $this->renderPartial('index');
    }
    public function actionLists()
    {
        $db=\yii::$app->db;
        $sql="select * from pay_ms";
        $arr=$db->createCommand($sql)->queryAll();
        exit(json_encode($arr,true));
    }

    public function actionDetail()
    {
        $req=\Yii::$app->request;
        $uid=$req->get("id",0);
        $pay_ms=PayMs::findOne(['id'=>$uid]);
        if ($pay_ms['type']==0){
            $pay_ms->type=1;
            $pay_ms->save(false);
        }
        else if($pay_ms['type']==1){
            $pay_ms->type=0;
            $pay_ms->save(false);
        }
    }
    public function actionAddForm(){
        return $this->renderPartial('add-form');
    }
    public function actionSetForm(){
        $req=\Yii::$app->request;
        $uid=$req->get("id",0);
        $list=PayValId::findOne(["id"=>$uid]);
        if (!$list){
            return $this->renderPartial('set-form',["id"=>$uid]);
        }else{
            return $this->renderPartial('set-form',["list"=>$list,"id"=>$uid]);
        }
    }
    public function actionSetSubmit(){
        $req=\Yii::$app->request->get();
        $db=\yii::$app->db;
        $sql="select id from pay_val_id where id=".$req['id'];
        $ars=$db->createCommand($sql)->queryOne();
        if ($ars){
            //UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值
            $sql="UPDATE pay_val_id SET value_id=:params where id=:id";
            $uids[':id'] = $req['id'];
            $uids[':params'] = json_encode($req['params']);
            $arr=$db->createCommand($sql,$uids)->execute();
        }else{
            $sql="INSERT INTO pay_val_id VALUES (null,:id,:params)";
            $uids[':id'] = $req['id'];
            $uids[':params'] = json_encode($req['params']);
            $arr=$db->createCommand($sql,$uids)->execute();
        }
        if ($arr){
        $code = 1;
        $msg = "操作成功！";
        }else{
         $code = 2;
         $msg = "操作失败！";
        }
        exit(json_encode(['code' => $code, 'msg' => $msg]));
    }
}
